<template>
  <div class="article-container">
    <el-dialog
      :title="title"
      :visible="addDialogVisible"
      width="30%"
      @close="closeaddDialogVisible"
    >
      <el-form ref="articleFormRef" size="small" label-width="100px" :model="articleForm" :rules="rules">
        <el-form-item label="文章标题：" prop="title">
          <el-input v-model="articleForm.title" placeholder="请输入文章标题" />
        </el-form-item>
        <el-form-item label="文章内容：" prop="articleBody">
          <quill-editor v-model="articleForm.articleBody" :options="editorOption" />
        </el-form-item>
        <el-form-item label="视频地址：" prop="videoURL">
          <el-input v-model="articleForm.videoURL" placeholder="请输入视频地址" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeaddDialogVisible">取 消</el-button>
        <el-button type="primary" @click="confirmArrticle()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, detail, update } from '../../api/hmmm/articles'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
  name: 'ArticlesAdd',
  components: {
    quillEditor
  },
  props: {
    // 对话框弹出的布尔值
    addDialogVisible: {
      type: Boolean,
      default: false
    },
    // 当前点击的文章的id
    editArticle: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      articleForm: {
        title: null,
        articleBody: null,
        videoURL: null
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      editorOption: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      }
    }
  },
  computed: {
    title() {
      return this.articleForm.id ? '修改文章' : '新增文章'
    }
  },
  methods: {
    // 关闭对话框
    closeaddDialogVisible() {
      this.$emit('update:addDialogVisible', false)
      this.$refs.articleFormRef.resetFields()
      this.articleForm = {
        title: null,
        articleBody: null,
        videoURL: null
      }
    },
    // 新增/修改
    confirmArrticle() {
      this.$refs.articleFormRef.validate(async(valid) => {
        if (valid) {
          // 修改
          if (this.articleForm.id) {
            await update(this.articleForm)
            // console.log(res)
            this.$message.success('修改文章成功')
          } else {
            await add(this.articleForm)
            this.$message.success('新增文章成功')
          }
          this.closeaddDialogVisible()
          this.$emit('updateArticle')
        }
      })
    },
    // 根据id查看文章详情
    async getArtiaclDetail(id) {
      const { data } = await detail({ id })
      // console.log(res)
      this.articleForm = data
      // console.log(this.articleId)
    }
  }
}
</script>

<style scoped lang='scss'>
.article-container {
  ::v-deep .ql-editor{
    height: 200px;
  }
  ::v-deep .ql-toolbar{
  padding: 0 8px;
}
}

</style>
